/**
*  Copyright 2009-2014 by WWW.KNET.CN  All rights reserved.
*/
package com.domain.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.filter.OncePerRequestFilter;

import com.domain.model.Sysuser;
import com.domain.util.commonUtil;

/**
 * TODO 过滤器
 * @author liujie
 * @date 2015年6月4日 上午9:34:11
 * 
 */
public class CommonFilter extends OncePerRequestFilter {

	private static final Logger logger = LoggerFactory.getLogger(CommonFilter.class);

	@Override
	protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		  // 不过滤的uri  
        String[] notFilter =  
            new String[] {"/images", "/js", "/css", "/ui","/login","/user/top"};  
        String url=req.getRequestURI();
        boolean doFilter=true;
        for (String s : notFilter) {
			if (url.indexOf(s)!= -1) {
				doFilter=false;//包含不过滤的url
				break;
			}
		}
        String contextPath=req.getContextPath(); 
        String basePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()+contextPath;
        if (doFilter)  
        {  
            // 执行过滤  
            // 从session中获取登录者实体  
            Sysuser obj = (Sysuser) req.getSession().getAttribute(commonUtil.CURRENTUSER);  
            logger.info("---------------------登录对象：{}",obj);
            if (null == obj)  
            {  
                boolean isAjaxRequest = isAjaxRequest(req);  
                if (isAjaxRequest)  
                {  
                    res.setCharacterEncoding("UTF-8");  
                    res.sendError(HttpStatus.UNAUTHORIZED.value(), "您已经太长时间没有操作,请刷新页面");  
                    return ;  
                }  
                res.sendRedirect(basePath+"/login/login.html");
                return;  
            }  
            else  
            {  
              req.getSession().setAttribute(commonUtil.CURRENTUSER, obj);
                // 如果session中存在登录者实体，则继续  
                chain.doFilter(req, res);  
            }  
        }  
        else  
        {  
            // 如果不执行过滤，则继续  
            chain.doFilter(req, res);  
        }
        
	}
	
    //判断是否为ajax请求
	 public static boolean isAjaxRequest(HttpServletRequest request)  
	    {  
	        String header = request.getHeader("X-Requested-With");   
	        if (header != null && "XMLHttpRequest".equals(header))   
	            return true;   
	        else   
	            return false;    
	    }  

	/*@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;

		HttpSession session = request.getSession();
		Sysuser account =(Sysuser) session.getAttribute(varUtil.CURRENTUSER);
		logger.info("==============当前登录对象：{}",account);
		if (account != null ) {
			if (null == account.getId()) {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/frame/login.jsp");
				dispatcher.forward(req, res);
			}else
			session.setAttribute(varUtil.CURRENTUSER, account);
		}else {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/frame/login.jsp");
			dispatcher.forward(req, res);
		}
	}
*/

}

